package com.iqiyi.pps.epg.core.utils;

import com.alibaba.fastjson.JSON;
import com.iqiyi.kiwi.utils.Configuration;
import com.iqiyi.pps.epg.core.model.channel.ChannelConfig;
import com.iqiyi.pps.epg.core.model.channel.SearchSettingConfig;
import com.iqiyi.pps.epg.core.service.channel.ChannelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by karkarrotxu on 2015/11/12.
 */
public final class SettingConfigUtil {
    private Logger logger = LoggerFactory.getLogger(SettingConfigUtil.class);
    public static final String PLATFORM_LIMIT = Configuration.getString("broadcast.config.platform.limit");
    private SearchSettingConfig config = null;
    private ChannelService channelService;
    private Map<Integer, String> regionMap = new HashMap<Integer, String>();
    private Map<Integer, String> onlineMap = new HashMap<Integer, String>();

    private static class LazyHolder {
        private static final SettingConfigUtil INSTANCE = new SettingConfigUtil();
    }

    private SettingConfigUtil() {
        if (config == null) {
            initDefaultConfig();
        }

    }

    public static final SettingConfigUtil getInstance() {
        return LazyHolder.INSTANCE;
    }


    private void initDefaultConfig() {
        ChannelService service = getChannelService();
        List<ChannelConfig> lists = service.getConfig("PlayPlatform");
        List<ChannelConfig> referlists = service.getConfig("PlatformRefer");
        PlatformSetting pSetting = null;
        List<Integer> platforms = JSON.parseArray(PLATFORM_LIMIT.replaceAll(";", ","), Integer.class);
        config = new SearchSettingConfig();
        for (ChannelConfig channelConfig : lists) {
            regionMap.put(channelConfig.getKey(), channelConfig.getVal());
        }
        for (ChannelConfig channelConfig : referlists) {
            onlineMap.put(channelConfig.getKey(), channelConfig.getVal());
        }
        for (ChannelConfig channelConfig : lists) {
            pSetting = new PlatformSetting();
            pSetting.setRegionName(regionMap.get(channelConfig.getKey()));
            pSetting.setOnlineName(onlineMap.get(channelConfig.getKey()));
        }
        for (Integer platform : platforms) {
            config.appendRegionLimitClient(Integer.valueOf(platform));
            config.appendOnlineLimitClient(Integer.valueOf(platform));
        }
        config.recordLimit();
    }

    private ChannelService getChannelService() {
        if (channelService == null) {
            channelService = (ChannelService) ApplicationContextUtil.getBean(ChannelService.class);
        }
        return channelService;
    }

    public SearchSettingConfig getDefault() {
        return config;
    }

    public Map<String, Object> transBySearchConfig(SearchSettingConfig config) {
        config.readLimit();
        Map<String, Object> params = new HashMap<String, Object>();
        switch (config.getCheckBossType()) {
            case SearchSettingConfig.USE_LIMIT:
                params.put("filter_INI_isPurchase", "1,2");
                break;
            case SearchSettingConfig.USE_ZERO:
                params.put("filter_INI_isPurchase", "0,-100");
                break;
        }
        if (config.getCheckCopyRightType() == SearchSettingConfig.USE_LIMIT) {
            params.put("filter_GTI_copyrightOwnerId", "0");
        }
        if (config.getCheckPublishType() == SearchSettingConfig.USE_LIMIT) {
            params.put("filter_EQI_pagePublishStatus", "1");
        }

        String onlinePrefix = "filter_EQI_";
        String regionPrefix = "region_";
        for (Integer clientKey : config.getNeedRegions()) {
            params.put(regionPrefix + regionMap.get(clientKey), "1");
        }
        for (Integer clientKey : config.getNeedOnlines()) {
            params.put(onlinePrefix + onlineMap.get(clientKey), "1");
        }
        return params;
    }

    public List<String> transToClientName(List<Integer> list) {
        List<String> returnList = new ArrayList<String>();
        for (Integer key : list) {
            if (regionMap.containsKey(key)) {
                returnList.add(regionMap.get(key));
            } else {
                logger.error("[transToClientName] [key={}] empty", key);
            }
        }
        return returnList;
    }

    static class PlatformSetting {
        private String onlineName;
        private String regionName;
        private boolean online;
        private boolean region;

        public String getOnlineName() {
            return onlineName;
        }

        public void setOnlineName(String onlineName) {
            this.onlineName = onlineName;
        }

        public String getRegionName() {
            return regionName;
        }

        public void setRegionName(String regionName) {
            this.regionName = regionName;
        }

        public boolean isOnline() {
            return online;
        }

        public void setOnline(boolean online) {
            this.online = online;
        }

        public boolean isRegion() {
            return region;
        }

        public void setRegion(boolean region) {
            this.region = region;
        }
    }
}
